/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cardarus.operatingsystem.struts.forms;

import com.cardarus.operatingsystem.domain.PaymentMethod;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Javier Snaider
 */
public class FinalPaymentActionForm extends org.apache.struts.action.ActionForm {

    private String paymentOption = "cash";

    public String getPaymentOption() {
        return paymentOption;
    }

    public void setPaymentOption(String paymentOption) {
        this.paymentOption = paymentOption;
    }
    private String cardNumber;

    public PaymentMethod getPaymentMethod() {

        if (paymentOption.equals("debit")) {
            return PaymentMethod.DEBIT_CARD;
        }
        if (paymentOption.equals("credit")) {
            return PaymentMethod.CREDIT_CARD;
        }
        return PaymentMethod.CASH;
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getPaymentMethod() != PaymentMethod.CASH) {
            if (getCardNumber() == null || getCardNumber().length() < 1) {
                errors.add("cardNumber", new ActionMessage("error.cardnumber.required"));               
            }
        }
        return errors;
    }

    /**
     * @return the cardNumber
     */
    public String getCardNumber() {
        return cardNumber;
    }

    /**
     * @param cardNumber the cardNumber to set
     */
    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }
}
